<template>
  <view class="container">
    <!-- <view class="alert">
      <text>安防告警</text>
    </view> -->
    <float-button name="security" />
    <view class="myForm">
      <uni-forms
        ref="valiForm"
        :rules="rules"
        v-model="formData"
        label-position="top"
        label-width="150"
        err-show-type="toast"
      >
        <uni-forms-item label="告警时间" required name="alarmTime">
          <uni-datetime-picker
            type="datetime"
            :end="new Date().getTime()"
            v-model="formData.alarmTime"
          />
        </uni-forms-item>
        <uni-forms-item label="空间位置" required name="spaceId">
          <uni-data-picker
            placeholder="请选择空间"
            popup-title="请选择"
            :localdata="treeData"
            v-model="formData.spaceId"
          >
          </uni-data-picker>
        </uni-forms-item>
        <uni-forms-item label="告警级别" required name="alarmLevel">
          <uni-data-select
            placeholder="请选择"
            :localdata="alarmLevel"
            v-model="formData.alarmLevel"
          />
        </uni-forms-item>
        <uni-forms-item label="告警内容" required name="alarmContent">
          <uni-easyinput
            type="textarea"
            v-model="formData.alarmContent"
            placeholder="请输入"
          />
        </uni-forms-item>
        <uni-forms-item label="附件" required name="oss">
          <FileUploader v-model="formData.oss" />
        </uni-forms-item>
      </uni-forms>
    </view>
    <view class="btnBox">
      <button class="btn" type="primary" @click="onSubmit('valiForm')">
        提交
      </button>
    </view>
  </view>
</template>

<script>
import { getSpaceList } from '@/api/index';
import { addSafeAlarm } from '@/api/alarm';
import FileUploader from '@/components/FileUploader/index.vue';
export default {
  components: {
    FileUploader,
  },
  data() {
    return {
      treeData: [],
      alarmLevel: [
        {
          text: '紧急告警',
          label: '紧急告警',
          value: '1',
        },
        {
          text: '重要告警',
          label: '重要告警',
          value: '2',
        },
        {
          text: '一般告警',
          label: '一般告警',
          value: '3',
        },
      ],
      // 表单数据
      formData: {
        oss: [],
        spaceId: '',
      },
      // 规则
      rules: {
        oss: {
          rules: [
            {
              required: true,
              errorMessage: '请上传附件',
            },
          ],
        },
        spaceId: {
          rules: [
            {
              required: true,
              errorMessage: '请选择位置',
            },
          ],
        },
        alarmTime: {
          rules: [
            {
              required: true,
              errorMessage: '请选择时间',
            },
          ],
        },
        alarmLevel: {
          rules: [
            {
              required: true,
              errorMessage: '请选择告警级别',
            },
          ],
        },
        alarmContent: {
          rules: [
            {
              required: true,
              errorMessage: '请输入告警内容',
            },
          ],
        },
      },
    };
  },
  watch: {
    'formData.date': {
      handler(newVal) {},
      immediate: false,
      deep: true,
    },
  },
  onLoad(query) {
    this.formData.alarmTime = this.$util.timestampToTime(new Date().getTime());
    getSpaceList().then((res) => {
      let data = res.data.map((item) => {
        return {
          ...item,
          text: item.spaceName,
          value: item.spaceId,
        };
      });
      this.treeData = this.$util.processingTree(data, 'spaceId', 'parentId');
    });
  },
  methods: {
    // 提交
    onSubmit(ref) {
      this.$refs[ref]
        .validate()
        .then((res) => {
          console.log('success', res);
          // loading提示
          uni.showLoading({ title: '提交中' });
          // 提交接口
          let params = {
            ...res,
            ossIdArrStr: res.oss.map((item) => item.ossId).join(),
          };
          addSafeAlarm(params).then((res) => {
            uni.hideLoading();
            uni.showToast({
              title: `提交成功`,
            });
            // 重置
            this.onReset();
          });
        })
        .catch((err) => {
          console.log('err', err);
        });
    },
    // 重置表单
    onReset() {
      this.formData = this.$options.data().formData;
    },
    // 跳转页面
    onLocation(url) {
      uni.navigateTo({
        url,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: calc(100vh - var(--window-bottom) - 200rpx);
  padding: 20rpx 36rpx;
  padding-bottom: calc(var(--window-bottom) + 200rpx);
  display: flex;
  flex-direction: column;

  .alert {
    padding: 16rpx;
    margin: 24rpx 0;
    text-align: center;
    background: #ddeafa;
    border-radius: 12rpx;
    text {
      font-size: 32rpx;
      font-weight: 600;
      color: #3a84ff;
    }
  }

  .myForm {
    padding: 24rpx;
    background: #fff;
    border-radius: 12rpx;

    ::v-deep .uni-forms-item__label {
      width: 35%;
      color: #333333;
      font-size: 28rpx;
      font-weight: bold;
      position: relative;

      &::after {
        // content: "";
        width: 100%;
        height: 1rpx;
        background: #000;
        position: absolute;
        left: 14rpx;
        bottom: 20rpx;
      }
    }

    .myLabel {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .text {
        color: #333333;
        font-size: 28rpx;
        font-weight: bold;
      }
      .star {
        color: #dd524d;
      }
    }
  }

  .btnBox {
    position: fixed;
    left: 36rpx;
    right: 36rpx;
    bottom: calc(var(--window-bottom) + 60rpx);
    .btn {
      width: 100%;
      height: 92rpx;
      font-size: 40rpx;
      color: #ffffff;
      line-height: 92rpx;
      background: #3681ff;
    }
  }
}
</style>
